<template>
  <div>
    <!-- 导航面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- closable 设置是否能关闭 -->
      <el-alert
        :closable="false"
        title="注意！只允许第三级分类设置相关参数"
        type="warning"
        show-icon
      >
      </el-alert>
      <el-row class="shop_classify">
        <el-col :span="2">
          <span>选择商品分类:</span>
        </el-col>
        <el-col :span="5">
          <el-cascader
            v-model="selectParamsList"
            :options="paramsList"
            :props="cascaderProps"
            @change="ClassifyhandleChange"
            :change-on-select="true"
            clearable
          ></el-cascader>
        </el-col>
      </el-row>

      <!-- tabs 标签页 -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            size="mini"
            :disabled="isDisabled"
            @click="adddialogVisible = true"
          >
            添加参数
          </el-button>
          <el-table :data="manyTabsData" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              prop="attr_name"
              label="参数名称"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  icon="el-icon-edit"
                  @click="editParams(scope.row.attr_id)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  @click="deleteParams(scope.row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button
            type="primary"
            size="mini"
            :disabled="isDisabled"
            @click="adddialogVisible = true"
          >
            添加属性
          </el-button>
          <el-table :data="onlyTabsData" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              prop="attr_name"
              label="参数名称"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  icon="el-icon-edit"
                  @click="editParams(scope.row.attr_id)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  @click="deleteParams(scope.row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-dialog
      :title="'添加' + titleData"
      :visible.sync="adddialogVisible"
      width="50%"
      @close="closeDialog"
    >
      <el-form
        :model="addForm"
        :rules="addrules"
        ref="addref"
        label-width="100px"
      >
        <el-form-item label="活动名称" prop="attr_name">
          <el-input v-model="addForm.attr_name"></el-input> </el-form-item
      ></el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addParams">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="'编辑' + titleData"
      :visible.sync="editdialogVisible"
      width="50%"
      @close="closeEditDialog"
    >
      <el-form
        :model="editForm"
        :rules="editrules"
        ref="editref"
        label-width="100px"
      >
        <el-form-item label="活动名称" prop="attr_name">
          <el-input v-model="editForm.attr_name"></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditParams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      paramsList: [], // 分类数据
      selectParamsList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
      },
      activeName: 'many',
      activeList: [],
      // 动态参数
      manyTabsData: [],
      // 静态属性
      onlyTabsData: [],
      adddialogVisible: false,
      editdialogVisible: false,
      addForm: {
        attr_name: '',
      },
      addrules: {
        attr_name: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      // 编辑
      editForm: {
        attr_name: '',
      },
      editrules: {
        attr_name: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  methods: {
    async getParamsList() {
      const { data } = await this.$http.get('categories')
      console.log('ss', data)
      if (data.meta.status !== 200) {
        this.$message.error('获取数据分类列表失败')
      }
      this.paramsList = data.data
    },
    ClassifyhandleChange() {
      this.getParamsData()
    },
    // 选中tabs标签页事件
    handleTabClick() {
      console.log(this.activeName)
      this.getParamsData()
    },
    async getParamsData() {
      // 如果选择的长度不是3，那么说明没有选择到最后一项
      if (this.selectParamsList.length !== 3) {
        // 清空数组
        this.selectParamsList = []
        // return 结束
        return
      }
      console.log(this.selectParamsList)
      const { data } = await this.$http.get(
        `categories/${this.cateId}/attributes`,
        {
          params: { sel: this.activeName },
        }
      )
      console.log('gg', data)
      if (this.activeName === 'many') {
        this.manyTabsData = data.data
      } else {
        this.onlyTabsData = data.data
      }
    },
    closeDialog() {
      // 清空 输入框内容
      this.$refs.addref.resetFields()
    },
    closeEditDialog() {
      this.$refs.editref.resetFields()
    },
    addParams() {
      this.$refs.addref.validate(async (valid) => {
        if (!valid) return
        const { data } = await this.$http.post(
          `categories/${this.cateId}/attributes`,
          {
            attr_name: this.addForm.attr_name,
            attr_sel: this.activeName,
          }
        )
        if (data.meta.status !== 201) {
          this.$message.error('请求添加失败')
        }
        this.$message.success('添加数据成功')
        this.getParamsData()
        this.adddialogVisible = false
      })
    },
    deleteParams(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
          const { data } = await this.$http.delete(
            `categories/${this.cateId}/attributes/${id}`
          )
          // console.log('aa', data)
          if (data.meta.status !== 200) {
            this.$message.error('删除失败')
          }
          this.$message.success('删除成功')
          this.getParamsData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    async editParams(id) {
      const { data } = await this.$http.get(
        `categories/${this.cateId}/attributes/${id}`,
        {
          params: {
            attr_sel: this.activeName,
          },
        }
      )
      if (data.meta.status !== 200) {
        this.$message.error('查询数据失败')
      }
      this.editForm = data.data
      console.log('vv', data)
      this.editdialogVisible = true
    },
    handleEditParams() {
      console.log(123)
      this.$refs.editref.validate(async (valid) => {
        if (!valid) return
        const { data } = await this.$http.put(
          `categories/${this.cateId}/attributes/${this.editForm.attr_id}`,
          {
            attr_name: this.editForm.attr_name,
            attr_sel: this.activeName,
          }
        )
        console.log('n', data)
        if (data.meta.status !== 200) {
          this.$message.error('编辑失败')
        }
        this.$message.success('修改成功')
        this.getParamsData()
        this.editdialogVisible = false
      })
    },
  },
  created() {
    this.getParamsList()
  },
  computed: {
    // 使用计算属性，根据上门级联选择器的length长度，需要禁用返回true,否则返回false
    isDisabled() {
      if (this.selectParamsList.length !== 3) {
        return true
      }
      return false
    },
    cateId() {
      if (this.selectParamsList.length === 3) {
        return this.selectParamsList[2]
      }
      return null
    },
    titleData() {
      if (this.activeName === 'many') {
        return '动态参数'
      } else {
        return '静态属性'
      }
    },
  },
}
</script>

<style lang="less" scoped>
.shop_classify {
  margin: 15px 0;
  span {
    line-height: 40px;
  }
}
</style>
